<?php
/**
 * Created by PhpStorm.
 * User: Steven
 * Date: 2017/6/21
 */

namespace app\course\model;


use app\classes\model\ClassModel;
use think\Db;
use think\Log;
use think\Model;

class CourseAdjustModel extends Model
{
    public function __construct($data = [])
    {
        parent::__construct($data);
    }

    /**
     * 获取调课列表
     * @param array $filter
     * @param array $order
     * @param int $limit
     * @param $query
     * @return \think\Paginator
     */
    public function getAdjust($filter=[], $order=[], $limit=10, $query)
    {
        //默认条件
        $where = [];
        if(!empty($filter) && is_array($filter)){
            $where = array_merge($where,$filter);
        }

        //默认排序
        $sort = ['id'=>'desc'];
        if(!empty($order) && is_array($order)){
            $sort = array_merge($order,$sort);
        }

        //分页url额外参数
        $page_query['query'] = [];
        if(!empty($query) && is_array($query)){
            $page_query['query'] = $query;
        }

        $data = $this->alias('ca')
            ->field('ca.*,sc.name as school_name,cl.name as class_name,cr.course_hour,cr.course_name')
            ->join('class cl','ca.class_id=cl.id')
            ->join('school sc','cl.school_id=sc.id')
            ->join('course cr','cl.course_id=cr.id')
            ->where($where)
            ->order($sort)
            ->paginate($limit,false,$page_query);

        return $data;
    }

    /**
     * 编辑调课
     * @param array $data
     * @return bool
     */
    public function editAdjust($data=[])
    {
        $result = true;
        $id = intval($data['id']);

        $oldAdjust = $this->where('id', $id)->find();
        if(!isset($oldAdjust->id)){
            $result = false;
        }else{
            self::startTrans();
            try{
                $this->isUpdate(true)->allowField(true)->save($data, ['id' => $id]);

                self::commit();
            }catch(\Exception $e){
                self::rollback();
                $result = false;
            }
        }

        return $result;
    }

    /**
     * 通过字段获取调课
     * @param array $filter
     * @return array|false|\PDOStatement|string|Model
     */
    public function getAdjustByField($filter=[])
    {
        //默认条件
        $where = [];
        if(!empty($filter) && is_array($filter)){
            $where = array_merge($where,$filter);
        }

        $obj = $this->where($where)->find();

        return $obj;
    }

    /**
     * @param $id
     * @param $cur_time
     * @return bool
     */
    public function deleteAdjust($id, $cur_time)
    {
        $result = true;
        if(empty($id)){
            $result = false;
        }else{
            if(is_array($id)){
                //批量删除
                self::startTrans();
                try{
                    foreach ($id as $value){
                        //获取班级
                        $class = $this->alias('ca')
                            ->field('cl.id,cl.name,cl.teacher_id')
                            ->join('class cl','ca.class_id=cl.id')
                            ->where('ca.id', $value)
                            ->find();
                        $schedule_mdl = new CourseScheduleModel();
                        //删除原有排课
                        $schedule_mdl->where(['class_id' => $class['id']])->where(['teacher_id' => $class['teacher_id']])->delete();

                        //获取排课
                        $class_mdl = new ClassModel();
                        $schedules = $class_mdl->getCourseTime($class['id']);
                        //重新排课
                        $schedule_total = count($schedules);
                        if($schedule_total > 0){
                            $new_data = [];
                            foreach ($schedules as $k=>$v){
                                $new_data[$k]['class_id'] = $v['class_id'];
                                $new_data[$k]['classroom_id'] = $v['classroom_id'];
                                $new_data[$k]['teacher_id'] = $v['teacher_id'];
                                $new_data[$k]['date'] = strtotime($v['date']);
                                $new_data[$k]['start_time'] = $v['start_time'];
                                $new_data[$k]['end_time'] = $v['end_time'];
                                $new_data[$k]['week'] = $v['cur_week'];
                                $new_data[$k]['schedule'] = $v['schedule'];
                            }
                            $schedule_mdl->addSchedule($new_data);
                        }
                    }

                    //批量删除调课信息
                    $this->where(['id' => ['in', $id]])->delete();
                    self::commit();
                }catch(\Exception $e){
                    self::rollback();
                    $result = false;
                }
            }else{
                //单一删除
                self::startTrans();
                try{
                    //获取班级
                    $class = $this->alias('ca')
                        ->field('cl.id,cl.name,cl.teacher_id')
                        ->join('class cl','ca.class_id=cl.id')
                        ->where('ca.id', $id)
                        ->find();
                    $schedule_mdl = new CourseScheduleModel();
                    //删除原有排课
                    $schedule_mdl->where(['class_id' => $class['id']])->delete();

                    //获取排课
                    $class_mdl = new ClassModel();
                    $schedules = $class_mdl->getCourseTime($class['id']);
                    //重新排课
                    $schedule_total = count($schedules);
                    if($schedule_total > 0){
                        $new_data = [];
                        foreach ($schedules as $k=>$v){
                            $new_data[$k]['class_id'] = $v['class_id'];
                            $new_data[$k]['classroom_id'] = $v['classroom_id'];
                            $new_data[$k]['teacher_id'] = $v['teacher_id'];
                            $new_data[$k]['date'] = strtotime($v['date']);
                            $new_data[$k]['start_time'] = $v['start_time'];
                            $new_data[$k]['end_time'] = $v['end_time'];
                            $new_data[$k]['week'] = $v['cur_week'];
                            $new_data[$k]['schedule'] = $v['schedule'];
                        }
                        $schedule_mdl->addSchedule($new_data);
                    }

                    //删除该调课
                    $this->where(['id' => $id])->delete();

                    self::commit();
                }catch(\Exception $e){
                    self::rollback();
                    $result = false;
                }
            }
        }

        return $result;
    }

}